<template>
  <eagle-popup
    title="选择隐患分类"
    position="bottom"
    :popStyle="{ height: '80%' }"
    v-bind="$attrs"
    v-on="$listeners"
    ref="eaglePopup"
    @open="onOpen"
    closeable
  >
    <div class="main">
      <el-tree
      :data="treeData"
      show-radio
      node-key="id"
      ref="tree"
      highlight-current
      :props="defaultProps"
      :expand-on-click-node="false"
      :default-expand-all=true
      @node-click="handleNodeClick"
    >
    </el-tree>
    </div>
  </eagle-popup>
</template>
<script>
export default {
  name: "danger-type",
  data() {
    return {
      data: [],
      setTree: [],
      defaultProps: {
        children: "children",
        label: "organName",
      },

      treeData: [],
      organList: [],
    };
  },
  created() {
    //加载部门管理节点接口定义
    // this.getQuerycheckList();
  },
  methods: {
    onOpen() {
      if (this.data.length == 0) {
        this.getData();
      }
    },
    getData() {
      this.$axios.get("/DangerType/GetTreeListData", null).then((res) => {
        this.data = res.data.List;
        this.getQuerycheckList()
      });
    },
    // 选择隐患分类
    handleclick(item, typeName) {
      item.DTypeName = typeName;
      item.DFullName = item.DTypeName+">"+item.DName;
      this.$emit("callback", item);
    },
     //部门管理节点接口定义
     getQuerycheckList() {
      this.dataLoading = true;
      let res2=
      {
    "success": true,
    "data": {
        "KeyField": "TypeCode",
        "ParentField": "TypePNo",
        "NameCaption": null,
        "CheckedField": "Checked",
        "IDField": "TypeNo",
        "NameField": "TypeName",
        "List": [
            {
                "TypeCode": "BM23080100001",
                "TypeName": "隐患大类1",
                "TypeNo": "0001",
                "TypePNo": "00",
                "TypeFullName": "隐患大类1",
                "TopTypeCode": "BM23080100001",
                "Order": null,
                "CompanyName": null,
                "ID": 9
            },
            {
                "TypeCode": "BM23080100002",
                "TypeName": "隐患大类2",
                "TypeNo": "0002",
                "TypePNo": "00",
                "TypeFullName": "隐患大类2",
                "TopTypeCode": "BM23080100002",
                "Order": null,
                "CompanyName": null,
                "ID": 10
            },
            {
                "TypeCode": "BM23080100003",
                "TypeName": "隐患二级大类1",
                "TypeNo": "000101",
                "TypePNo": "0001",
                "TypeFullName": "隐患大类1>隐患二级大类1",
                "TopTypeCode": "BM23080100001",
                "Order": null,
                "CompanyName": null,
                "ID": 11
            },
            {
                "TypeCode": "BM23080100004",
                "TypeName": "隐患二级大类1",
                "TypeNo": "000201",
                "TypePNo": "0002",
                "TypeFullName": "隐患大类2>隐患二级大类1",
                "TopTypeCode": "BM23080100002",
                "Order": null,
                "CompanyName": null,
                "ID": 12
            },
            {
                "TypeCode": "BM23080100005",
                "TypeName": "隐患分类",
                "TypeNo": "00020101",
                "TypePNo": "000201",
                "TypeFullName": "隐患大类2>隐患二级大类1>隐患分类",
                "TopTypeCode": "BM23080100002",
                "Order": null,
                "CompanyName": null,
                "ID": 13
            },
            {
                "TypeCode": "BM23080100006",
                "TypeName": "隐患分类",
                "TypeNo": "00010101",
                "TypePNo": "000101",
                "TypeFullName": "隐患大类1>隐患二级大类1>隐患分类",
                "TopTypeCode": "BM23080100001",
                "Order": null,
                "CompanyName": null,
                "ID": 14
            },
            {
                "TypeCode": "BM23080100020",
                "TypeName": "隐患分类1",
                "TypeNo": "00020102",
                "TypePNo": "000201",
                "TypeFullName": "隐患大类2>隐患二级大类1>隐患分类1",
                "TopTypeCode": "BM23080100002",
                "Order": null,
                "CompanyName": null,
                "ID": 28
            },
            {
                "TypeCode": "BM23080100007",
                "TypeName": "隐患分类2",
                "TypeNo": "00010102",
                "TypePNo": "000101",
                "TypeFullName": "隐患大类1>隐患二级大类1>隐患分类2",
                "TopTypeCode": "BM23080100001",
                "Order": null,
                "CompanyName": null,
                "ID": 15
            },
            {
                "TypeCode": "BM23080100021",
                "TypeName": "隐患分类",
                "TypeNo": "0002010201",
                "TypePNo": "00020102",
                "TypeFullName": "隐患大类2>隐患二级大类1>隐患分类1>隐患分类",
                "TopTypeCode": "BM23080100002",
                "Order": null,
                "CompanyName": null,
                "ID": 29
            },
            {
                "TypeCode": "BM23080100024",
                "TypeName": "隐患分类2",
                "TypeNo": "0002010202",
                "TypePNo": "00020102",
                "TypeFullName": "隐患大类2>隐患二级大类1>隐患分类1>隐患分类2",
                "TopTypeCode": "BM23080100002",
                "Order": null,
                "CompanyName": null,
                "ID": 32
            },
            {
                "TypeCode": "-1",
                "TypeName": "黄江新美洋",
                "TypeNo": "00",
                "TypePNo": "-1",
                "TypeFullName": "黄江新美洋",
                "TopTypeCode": "-1",
                "Order": null,
                "CompanyName": null,
                "ID": 0
            }
        ]
    },
    "errorType": 0,
    "errorText": null,
    "code": 0
}

// this.$axios.get("/DangerType/GetTreeListData", null).then((res) => {
//         this.data = res.data.List;
//         console.log(res.data.List)
//       });
    let organList=[]
    console.log(res2.data.List)
    // this.setTree=res2.data.List.map(a=>{
    //   return {
    //   parentId:a.TypePNo,
    //   organName:a.TypeName,
    //   id:a.ID
    //   }
    // })
    // res2.data.List.forEach((a)=>{
    this.data.forEach((a)=>{
      let ttt=a
      // ttt.parentId=a.TypePNo,
      // ttt.organName=a.TypeName,
      // ttt.id=a.TypeNo

      a.parentId=a.TypePNo,
      a.organName=a.TypeName,
      a.id=a.TypeNo

       this.setTree.push(a)
    })


    

    // console.log(this.setTree)
    // this.organList = res2.data.List.map((a) => ({
    //       label: a.TypeName,
    //       value: a.TypeNo,
    //     }));
        //debugger;
        console.log(this.setTree)
        this.getListData();
        this.dataLoading = false;
    },
    getListData() {
      let dataArray = [];
      this.setTree.forEach(function (data) {
        //  debugger;
        let parentId = data.parentId;
        if (parentId === "-1") {
          // let objTemp = {
          //   id: data.id,
          //   organName: data.organName,
          //   manager: data.manager,
          //   phone: data.manager,
          //   parentId: parentId,
          // };
          data.id= data.id,
          data.organName= data.organName,
          data.parentId=parentId,
          dataArray.push(data);
        }
      });
      this.treeData = this.data2treeDG(this.setTree, dataArray);
    },
    data2treeDG(datas, dataArray) {
      for (let j = 0; j < dataArray.length; j++) {
        let dataArrayIndex = dataArray[j];
        let childrenArray = [];
        let Id = dataArrayIndex.id;
        for (let i = 0; i < datas.length; i++) {
          let data = datas[i];
          let parentId = data.parentId;
          if (parentId == Id) {
            //判断是否为儿子节点
            // let objTemp = {
            //   id: data.id,
            //   organName: data.organName,
            //   manager: data.manager,
            //   phone: data.phone,
            //   parentId: parentId,
            // };

            data.id= data.id,
          data.organName= data.organName,
          data.parentId=parentId,
            // childrenArray.push(objTemp);
            childrenArray.push(data);
          }
        }
        dataArrayIndex.children = childrenArray;
        if (childrenArray.length > 0) {
          this.data2treeDG(datas, childrenArray);
        }
      }
      return dataArray;
    },
    handleNodeClick(e, node) {
        console.log(e)
        console.log(node)
        let item={};
        // item.DCode=e.TypeCode
        // item.DName=e.TypeName
        // item.DType=e.TopTypeCode
        // item.DTypeName = e.TypeName;
        // item.DFullName=e.TypeFullName
        item.TypeCode=e.TypeCode
        item.TypeName=e.TypeName
        item.TopTypeCode=e.TopTypeCode
        item.TypePNo=e.TypePNo
        item.TypeFullName=e.TypeFullName
        this.$emit("callback",item );
      },
  },
};
</script>

<style lang="less" scoped>
.main {
  background-color: #e8f1f8;
}
.title {
  border-left: 3px solid rgb(0, 195, 224);
  padding: 10px;
}
.lgdtype-bd {
  background-color: #fff;
  padding-top:10px;
  padding-bottom: 10px;
}
.d-name {
  width: 105px;
  border: 1px solid #eee;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 5px;
  font-size: 14px;
  text-align: center;
}
.el-tree-node__label {
width: auto;
margin-top: 12px;
float: right;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
font-size: 50px;
color: #999999;
line-height: 25px;
letter-spacing: 1px;
}
.el-tree-node:focus>.el-tree-node__content{
      background-color: #5daaf0;   //背景色
    }
</style>